package com.grade.controller;

import com.grade.Beans.GradeSum;
import com.grade.service.StudentService;
import com.grade.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tongji")
public class TongjiController {
    @Autowired
    StudentService studentService;
    @RequestMapping("/teacherTongji")
    @ResponseBody
    public Results teacherTongji() {
        // 获取学生成绩汇总列表
        List<GradeSum> gradeSums = studentService.scoressum();
        // 创建两个数组分别存储名字和成绩，长度与集合一致
        String[] names = new String[gradeSums.size()];
        double[] scores = new double[gradeSums.size()];
        // 遍历集合，填充数组
        for (int i = 0; i < gradeSums.size(); i++) {
            GradeSum gradeSum = gradeSums.get(i);
            names[i] = gradeSum.getStudentName();  // 存储名字
            scores[i] = gradeSum.getScore();       // 存储成绩
        }
        // 创建一个容器存储两个数组（使用Map更清晰）
        Map<String, Object> data = new HashMap<>();
        data.put("studentName", names);  // 名字数组
        data.put("score", scores);       // 成绩数组

        // 将封装好的数据传入Results
        return new Results("0", "查询成功", data, null);
    }
    @RequestMapping("/studentTongji")
    @ResponseBody
    public Results studentTongji(HttpSession session) {
        int studentNo=(Integer) session.getAttribute("id");
        List<GradeSum> gradeSums = studentService.scoresstudent(studentNo);
        // 创建两个数组分别存储名字和成绩，长度与集合一致
        String[] names = new String[gradeSums.size()];
        double[] scores = new double[gradeSums.size()];
        // 遍历集合，填充数组
        for (int i = 0; i < gradeSums.size(); i++) {
            GradeSum gradeSum = gradeSums.get(i);
            names[i] = gradeSum.getStudentName();  // 存储名字
            scores[i] = gradeSum.getScore();       // 存储成绩
        }
        // 创建一个容器存储两个数组（使用Map更清晰）
        Map<String, Object> data = new HashMap<>();
        data.put("name", names);  // 名字数组
        data.put("score", scores);       // 成绩数组

        // 将封装好的数据传入Results
        return new Results("0", "查询成功", data, null);
    }
}
